package com.liukai.bishe.ui.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Vibrator;

import com.liukai.bishe.R;
import com.liukai.bishe.ui.activity.MainActivity;

import java.util.Date;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import static com.liukai.bishe.App.CHANNEL_ID;

public class BiSheService extends Service {

    private Vibrator vibrator=null;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String title = intent.getStringExtra("title");
        vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
//        vibrator.vibrate(3500);
        new Thread(new Runnable() {
            @Override
            public void run() {
                vibrator.vibrate(new long[]{70, 100, 50, 500}, 0);
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                vibrator.cancel();
            }
        }).start();

        Intent notificationIntent = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0,notificationIntent,0);

        Date date = new Date();
        @SuppressLint("WrongConstant")
        Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
                .setContentTitle(title)
                .setContentText("时间到")
                .setBadgeIconType(R.drawable.ic_sms_black_24dp)
                .setSmallIcon(R.drawable.ic_sms_black_24dp)
                .setContentIntent(pendingIntent)
                .setTimeoutAfter(date.getTime())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(date.getTime())
                .setNumber(1111)
                .build();
                startForeground(1,notification);

        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
